package com.sportizz.sport.row;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sportizz.model.ImageLoader;
import com.sportizz.model.player_info;
import com.sportizz.sport.Br_custom_tweet_list;
import com.sportizz.sport.Custom_dialog_class;
import com.sportizz.sport.DatabaseAdapter;
import com.sportizz.sport.MainActivity;
import com.sportizz.sport.MainMenu;
import com.sportizz.sport.R;
import com.sportizz.sport.SubMenu;
import com.sportizz.sport.R.color;
import com.sportizz.sport.StartActivity;
import com.sportizz.sport.Utils;
import com.sportizz.sport.st_tweet_MainMenu;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;

public class new_Br_custom_player_list extends ArrayAdapter<player_info> {

	Activity context;
	ArrayList<String> sport_team_adp;
	HashMap<String, String> team_code;
	LayoutInflater lif;
	String[] code;
	String country_code;
	List<player_info> player_adp;
	private boolean bpress = false;
	public String response = null; 
	public Long ins1_c;
	public ProgressDialog pd;
	public WindowManager mWindowManager;

	StartActivity sa = new StartActivity();
	ImageLoader imgLoader;
	Context con;

	public new_Br_custom_player_list(Context _context, List<player_info> player_adp) {

		super(_context, R.layout.br_team_custom_follow, player_adp);
		
		context = (Activity) _context;

		this.con = _context;

		this.player_adp = player_adp;

		mWindowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);

		imgLoader = new ImageLoader(con.getApplicationContext());
	}

	static class ViewHolder {

		protected TextView br_set_team_name;
		protected Button br_set_team_follow;
		protected Button br_set_team_tweet;
		protected ImageView team_image;

//		public ViewHolder(TextView br_set_team_name, Button br_set_team_follow,
//				ImageView team_image, Button br_set_team_tweet) {
//
//			this.br_set_team_name = br_set_team_name;
//			this.br_set_team_follow = br_set_team_follow;
//			this.team_image = team_image;
//			this.br_set_team_tweet = br_set_team_tweet;
//		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		LayoutInflater inflator = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final ViewHolder viewHolder;

		if (convertView == null) {
			convertView = inflator.inflate(R.layout.br_team_custom_follow, null);
			viewHolder = new ViewHolder();
			
			viewHolder.br_set_team_name =(TextView) convertView
					.findViewById(R.id.br_set_team_name);
			viewHolder.br_set_team_follow =(Button) convertView
					.findViewById(R.id.br_set_team_follow);
			viewHolder.team_image =(ImageView) convertView
					.findViewById(R.id.br_set_image);
			viewHolder.br_set_team_tweet = (Button) convertView
					.findViewById(R.id.team_tweet);

			convertView.setTag(viewHolder);
			
			viewHolder.br_set_team_tweet.setVisibility(View.INVISIBLE);
			

			
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		Typeface sport_font = Typeface.createFromAsset(con.getAssets(),
				"hel57.ttf");

		viewHolder.br_set_team_follow.setTag(player_adp.get(position).getplayerCode());

		viewHolder.br_set_team_name.setText(player_adp.get(position).getPlayerName());

		viewHolder.br_set_team_name.setTypeface(sport_font);

		viewHolder.br_set_team_name.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

		Log.d("suraj", "player_adp.get(position).getPlayerName()="
				+ player_adp.get(position).getPlayerName());

		 String player_country = "" + StartActivity.get_img_location
					+ "pad/country/" + player_adp.get(position).getnational()
					+ "@2x.png";

			int loader = getImgLoader(player_adp.get(position).getsportCode());
			
			imgLoader.DisplayImage(player_country,loader,viewHolder.team_image);		
		

			viewHolder.br_set_team_follow.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {

					final DatabaseAdapter mDbHelper = new DatabaseAdapter(context);
					mDbHelper.open();

					StartActivity.foll_count = StartActivity.follow_count.getInt(
							"follow_count", 0);

					Log.v("suraj", "StartActivity.foll_count = "
							+ player_adp.get(position)
							.getPlayerName());

					// if (StartActivity.foll_count < StartActivity.limited_follow
					// .getInt("max_follow", 3)
					// && StartActivity.foll_count >= 0) {

					if (viewHolder.br_set_team_follow.getText().toString()
							.equalsIgnoreCase("FOLLOW")) {

						AsyncTask<Void, Void, Void> wait = new AsyncTask<Void, Void, Void>() {

							@Override
							protected void onPostExecute(Void result) {
								super.onPostExecute(result);
								pd.dismiss();

							}

							@Override
							protected void onPreExecute() {

								pd = ProgressDialog.show(context, "",
										"Following...", true);
								super.onPreExecute();
							}

							@Override
							protected Void doInBackground(Void... params) {
								try {
									String device_id = sa.GetDeviceID(context);

									String national = null;

									if (player_adp
											.get(position)
											.getnational()
											.equals(player_adp.get(position)
													.getss_club5())) {
										national = player_adp.get(position)
												.getnational();
									} else {
										national = player_adp.get(position)
												.getplayerCode();
									}

									ins1_c = mDbHelper
											.insert_follow_info(
													player_adp.get(position)
															.getplayerCode(),
													player_adp.get(position)
															.getPlayerName(),
													player_adp.get(position)
															.getsportCode(),
													player_adp.get(position)
															.getSportName(),
													national,
													player_adp.get(position)
															.getplayeris_team(),
													"P");

									response = Utils
											.getJSONString("http://sportizza.com/interface/ios_follow.php?dev_id="
													+ device_id
													+ "&iso_cd="
													+ country_code
													+ "&follow="
													+ player_adp.get(position)
															.getplayerCode()
													+ "&follow_cs=-1");
								} catch (Exception e) {

								}

								if (response != null || ins1_c != 0) {

									Thread t = new Thread(new Runnable() {
										public void run() {

											context.runOnUiThread(new Runnable() {
												public void run() {

													Toast.makeText(
															context.getApplicationContext(),
															"Added to I follow",
															Toast.LENGTH_SHORT)
															.show();
													Log.d("suraj",
															"follow success..");

													viewHolder.br_set_team_follow
															.setText("UNFOLLOW");

													MainActivity.playerAll
															.add(player_adp
																	.get(position)
																	.getplayerCode());

													Log.d("suraj",
															"PLAYER = "
																	+ MainActivity.playerAll);

													StartActivity.foll_count++;

													SharedPreferences.Editor editor = StartActivity.follow_count
															.edit();
													editor.putInt(
															"follow_count",
															StartActivity.foll_count);
													editor.commit();
												}
											});
										}
									});
									t.start();

								} else {
									Toast.makeText(context.getApplicationContext(),
											"Error during follow",
											Toast.LENGTH_SHORT).show();
								}

								bpress = true;
								mDbHelper.close();

								return null;
							}

						};
						wait.execute();

					}
					// ///////////


				}

			});
			
		if (MainActivity.playerAll != null) {
			if (MainActivity.playerAll.contains(viewHolder.br_set_team_follow.getTag()
					.toString()) == true) {
				viewHolder.br_set_team_follow.setText("UNFOLLOW");
			} else {
				viewHolder.br_set_team_follow.setText("FOLLOW");
			}

		}
		
		return convertView;
	}

	public int getImgLoader(String sport_code) {

		int loader = R.drawable.gam;

		if (sport_code.equalsIgnoreCase("AFT")) {
			loader = R.drawable.aft;
		} else if (sport_code.equalsIgnoreCase("TT")) {
			loader = R.drawable.tt;
		} else if (sport_code.equalsIgnoreCase("POL")) {
			loader = R.drawable.pol;
		} else if (sport_code.equalsIgnoreCase("SWI")) {
			loader = R.drawable.swi;
		} else if (sport_code.equalsIgnoreCase("UFC")) {
			loader = R.drawable.ufc;
		} else if (sport_code.equalsIgnoreCase("BAD")) {
			loader = R.drawable.bad;
		} else if (sport_code.equalsIgnoreCase("BB")) {
			loader = R.drawable.bb;
		} else if (sport_code.equalsIgnoreCase("CRI")) {
			loader = R.drawable.cri;
		} else if (sport_code.equalsIgnoreCase("F1")) {
			loader = R.drawable.f1;
		} else if (sport_code.equalsIgnoreCase("GAM")) {
			loader = R.drawable.gam;
		} else if (sport_code.equalsIgnoreCase("GOL")) {
			loader = R.drawable.gol;
		} else if (sport_code.equalsIgnoreCase("HOC")) {
			loader = R.drawable.hoc;
		} else if (sport_code.equalsIgnoreCase("ICE")) {
			loader = R.drawable.ice;
		} else if (sport_code.equalsIgnoreCase("IND")) {
			loader = R.drawable.ind;
		} else if (sport_code.equalsIgnoreCase("MGP")) {
			loader = R.drawable.mgp;
		} else if (sport_code.equalsIgnoreCase("NAS")) {
			loader = R.drawable.nas;
		} else if (sport_code.equalsIgnoreCase("OLY")) {
			loader = R.drawable.oly;
		} else if (sport_code.equalsIgnoreCase("RUG")) {
			loader = R.drawable.rug;
		} else if (sport_code.equalsIgnoreCase("SBK")) {
			loader = R.drawable.sbk;
		} else if (sport_code.equalsIgnoreCase("SOC")) {
			loader = R.drawable.soc;
		} else if (sport_code.equalsIgnoreCase("SPO")) {
			loader = R.drawable.spo;
		} else if (sport_code.equalsIgnoreCase("SQU")) {
			loader = R.drawable.squ;
		} else if (sport_code.equalsIgnoreCase("TEN")) {
			loader = R.drawable.ten;
		} else if (sport_code.equalsIgnoreCase("V8C")) {
			loader = R.drawable.v8c;
		} else if (sport_code.equalsIgnoreCase("WRC")) {
			loader = R.drawable.wrc;
		} else {
			loader = R.drawable.gam;
		}
		return loader;
	}
	
}
